package dataProcess;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;

import ds.BugInfo;

public class BugInfoToTextFile {
	
	/**
	 * a line is a bug
	 * @param bugs
	 */
	public static void bug2TextFile(ArrayList<BugInfo> bugs, String fileName){
		try {
			FileWriter fw = new FileWriter(fileName);
			fw.write(bugs.size()+"\n");
			for(int i=0; i<bugs.size(); i++){
				BugInfo bug = bugs.get(i);
				String line = bug.bugID + " " + bug.content + "\n";
				fw.write(line);
			}
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	
	public static void bugList2FileList(ArrayList<BugInfo> bugs, String outDir){
		File dir = new File(outDir);
		if(!dir.exists())
			dir.mkdir();
		else{
			File[] files = dir.listFiles();  
			for(File file : files){
				if(!file.delete()) {     	
					System.out.println("[ERROR] failed to delete file " + file.getPath());
					break;	
				}
			}
		}
		
		for(int i = 0; i < bugs.size(); i++){
			BugInfo curBug = bugs.get(i);
			String fileName = curBug.bugID;
			try {
				PrintStream ps = new PrintStream(outDir + fileName);
				ps.println(curBug.content);
				ps.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
		}
	}
	
	
}
